package com.zhiwei.common.exception;

import com.zhiwei.common.constant.ErrorEnum;
import com.zhiwei.common.constant.ResultResponseMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ZHIWEI.YANG
 * @createtime 2019/2/1 - 17:02
 * @decription 异常捕获全局类
 **/
@ControllerAdvice
@Slf4j
public class GlobleExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResultResponseMsg errorMsg(HttpServletRequest request, Exception ex) {
        log.error("===> HTTP请求访问路径异常URL: {}", request.getRequestURL(), ex);

        //自定义异常处理
        if (ex instanceof BasePublishException) {
            return ResultResponseMsg.buildResultMsg(((BasePublishException) ex).getCode(), ex.getMessage(), null);
            //通用异常处理
        } else {
            return ResultResponseMsg.buildResultMsg(ErrorEnum.UNKNOWN, null);
        }
    }
}
